package gui.game;
import game.GameBoard;
import game.GameField;
import game.GameLogic;
import game.PlayerDTO;
import gui.components.PlayerPanel;
import gui.dialogs.GameDialog;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import core.Controller;

import utility.User;
import utility.WindowManipulator;




public class GUI extends JFrame{
	
	private ImageIcon gameBoardLogo;
	private JLabel gameBoardLogoLabel;
	private Container c;
	private JButton dices, finishTurn, buyStreet;
	private JMenuBar menuBar;
	private JMenu gameMenu, helpMenu;
	private JMenuItem exitItem, finishItem, buyItem, dicesItem, helpItem;
	private GameBoard gameboard;
	private static GUI INSTANCE=null;
	private GameLogic gl;
	private PlayerDTO pl;
	private PlayerPanel player1Pnl;
	private PlayerPanel player2Pnl;
	private int scrResHigh = (int) (User.getScreenResolution().height *0.92);
	private int scrResWidth = (int) (User.getScreenResolution().width *0.98);
	private static int SIZE=GUI.HUGE;
	private static final int SMALL=2;
	private static final int HUGE=1;

	
	public GUI() throws Exception{
		GUI.INSTANCE=this;
		c = getContentPane();
		c.setLayout(null);
		c.setPreferredSize(new Dimension(scrResWidth,scrResHigh));
		
		checkScreenSize();
		
		gameboard=GameBoard.getInstance();
		gl=GameLogic.getInstance();
		pl=PlayerDTO.getInstance();
		
		//create the gameboard without painting
		createGameBoard(1360/SIZE,930/SIZE);
		
		//create Players and paint player
		
		pl.setPlayerFigures();
		paintPlayerPosition();
		c.add(pl.getPlayer1());
		c.add(pl.getPlayer2());
		
		finishItem = new JMenuItem("Finish turn");
		exitItem = new JMenuItem("Exit game");
		dicesItem = new JMenuItem("Roll dices");
		buyItem = new JMenuItem("Buy street");
		helpItem = new JMenuItem("Show help");
		
		//dices button
		dices = new JButton();
		dices.setIcon(new ImageIcon("images/"+gl.getFolderName()+"dices1.png"));
//		dices.setSize(new Dimension(100,200));
		dices.setBounds(1500/SIZE, 180/SIZE, 100/SIZE, 100/SIZE);
		dices.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Random random = new Random();
				pl.getCurrentPlayer().setDices(random.nextInt(10)+2);
				player1Pnl.repaintPanel();
				player2Pnl.repaintPanel();				
				dices.setEnabled(false);
				dicesItem.setEnabled(false);
				gl.startGL();
			}
		});
		c.add(dices);
		
		//finish turn Button
		finishTurn = new JButton();
		finishTurn.setIcon(new ImageIcon("images/"+gl.getFolderName()+"finish_turn.png"));
		finishTurn.setBounds(1620/SIZE, 180/SIZE, 100/SIZE, 75/SIZE);
		finishTurn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buyStreet.setEnabled(false);
				buyItem.setEnabled(false);
				disableBuyButton();
				gl.nextPlayer();
			}
		});
		c.add(finishTurn);
		
		// buy button
		buyStreet = new JButton();
		buyStreet.setIcon(new ImageIcon("images/"+gl.getFolderName()+"buy_button.png"));
		buyStreet.setBounds(1500/SIZE, 300/SIZE, 100/SIZE, 100/SIZE);
		disableBuyButton();
		buyStreet.addActionListener( new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
				GameField gf = gameboard.getGameBoard().get(pl.getCurrentPlayer().getField());
				if (gl.buyStreet(gf)){						
						gf.setOwner(pl.getCurrentPlayer());
						JLabel square = new JLabel();
						square.setOpaque(true);
						square.setBackground(pl.getCurrentPlayer().getColor());
						if (gf.getOrientation()==GameField.UP){
							square.setBounds(gf.getCenter().x - (gf.getPreferredSize().width/2),
									gf.getCenter().y + (gf.getPreferredSize().height/2),
									gf.getPreferredSize().width, 
									20/SIZE);
						}else if (gf.getOrientation()==GameField.RIGHT){
							square.setBounds(gf.getCenter().x - (gf.getPreferredSize().width/2) - 20/SIZE,
									gf.getCenter().y - (gf.getPreferredSize().height/2),
									20/SIZE, 
									gf.getPreferredSize().height);
						}else if (gf.getOrientation()==GameField.DOWN){
							square.setBounds(gf.getCenter().x - (gf.getPreferredSize().width/2),
									gf.getCenter().y - (gf.getPreferredSize().height/2) - 20/SIZE,
									gf.getPreferredSize().width, 
									20/SIZE);
						}else {
							square.setBounds(gf.getCenter().x + (gf.getPreferredSize().width/2),
									gf.getCenter().y - (gf.getPreferredSize().height/2),
									20/SIZE, 
									gf.getPreferredSize().height);
						}
							c.add(square);
							square.repaint();
							disableBuyButton();
						
						
					} 
				}	catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		c.add(buyStreet);
		
		
		
		// add Player Infos
		
		player1Pnl = new PlayerPanel(pl.getPlayer1());
		player2Pnl = new PlayerPanel(pl.getPlayer2());
		
		player1Pnl.setBounds(10/SIZE, 50/SIZE, 320/SIZE, 200/SIZE);
		player2Pnl.setBounds(10/SIZE,260/SIZE, 320/SIZE, 200/SIZE);
		
		c.add(player1Pnl);
		c.add(player2Pnl);
		
		
		menuBar = new JMenuBar();
		gameMenu = new JMenu("Game");
		helpMenu = new JMenu("Help");
		
		helpItem.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("aaa");
				GameDialog.getHelpDialog(GUI.getInstance());
			}
		});
		
		finishItem.addActionListener(finishTurn.getActionListeners()[0]);
		
		exitItem.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {				
				Controller.exit();
			}
		});
		
		dicesItem.addActionListener(dices.getActionListeners()[0]);
		
		buyItem.addActionListener(buyStreet.getActionListeners()[0]);
		
		menuBar.add(gameMenu);
		menuBar.add(helpMenu);
		gameMenu.add(dicesItem);
		gameMenu.add(buyItem);
		gameMenu.add(finishItem);
		gameMenu.add(exitItem);
		helpMenu.add(helpItem);
		
		this.setJMenuBar(menuBar);
		
		
		
		
		
		WindowManipulator.addWindowIcon(this);
		this.setTitle("Monopoly Clone    -     by Dennis Windmeier and Marek Modry");
		
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		paintGameBoard();
		pack();
		this.setVisible(true);
		
	}
	private void checkScreenSize() {
		if (scrResHigh > 800 && scrResWidth > 1400){
			GUI.SIZE=GUI.HUGE;
			
		}else{
			GUI.SIZE=GUI.SMALL;
			GameLogic.getInstance().setFolderName("Small/");
		}
		
	}
	public static GUI getInstance(){
		if (GUI.INSTANCE==null){
			try {
				new GUI();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return GUI.INSTANCE;
	}
	
	public void repaintPlayersInfo(){
		
		player1Pnl.repaintPanel();
		player2Pnl.repaintPanel();
	}
	

	
	
	private void createGameBoard(int inX, int inY) throws Exception{
		if (GUI.SIZE==SMALL)
			GameBoard.getInstance().imageFolder= "images/Small/UK Fields/";
		
		GameBoard.getInstance().createGameBoard();
		
		ArrayList<GameField> gameBoard=GameBoard.getInstance().getGameBoard();
		
		
		
		
		GameField gameField=null;
		int x=inX;
		int y=inY;
		for(int i=0;i<11;i++){
			gameField=gameBoard.get(i);
			x-=gameField.getPreferredSize().width;
			gameField.setBounds(x, y, gameField.getPreferredSize().width, gameField.getPreferredSize().height);
//			c.add(gameField);
		}
		for (int i=11;i<21;i++){
			gameField=gameBoard.get(i);
			y-=gameField.getPreferredSize().height;
			gameField.setBounds(x, y, gameField.getPreferredSize().width, gameField.getPreferredSize().height);
//			c.add(gameField);
		}
		x+=gameField.getPreferredSize().width;
		y+=gameField.getPreferredSize().height;
		gameBoardLogo=new ImageIcon(GameBoard.imageFolder+"gameBoard.png");
		gameBoardLogoLabel= new JLabel(gameBoardLogo);
		gameBoardLogoLabel.setBounds(x, y, gameBoardLogoLabel.getPreferredSize().width, gameBoardLogoLabel.getPreferredSize().height);
		c.add(gameBoardLogoLabel);
		y-=gameField.getPreferredSize().height;
		for (int i=21;i<31;i++){
			gameField=gameBoard.get(i);
			gameField.setBounds(x, y, gameField.getPreferredSize().width, gameField.getPreferredSize().height);
			x+=gameField.getPreferredSize().width;
//			c.add(gameField);
		}
		x-=gameField.getPreferredSize().width;
		y+=gameField.getPreferredSize().height;
		for (int i=31;i<40;i++){
			gameField=gameBoard.get(i);
			gameField.setBounds(x, y, gameField.getPreferredSize().width, gameField.getPreferredSize().height);
			y+=gameField.getPreferredSize().height;
//			c.add(gameField);
		}
		
		
	}
	
	public void paintPlayerPosition() throws Exception{
		Point posPlayer1 = gameboard.getGameBoard().get(pl.getPlayer1().getField()).getCenter();
		Point posPlayer2 = gameboard.getGameBoard().get(pl.getPlayer2().getField()).getCenter();
		int i = pl.getPlayer1().getPreferredSize().width;
		if (pl.getPlayer1().getField()==pl.getPlayer2().getField()){
			pl.getPlayer1().setBounds(posPlayer1.x - (pl.getPlayer1().getCenter().x), 
					posPlayer1.y - (pl.getPlayer1().getCenter().y) - pl.getPlayer2().getPreferredSize().height/2, 
					pl.getPlayer1().getPreferredSize().width, 
					pl.getPlayer1().getPreferredSize().height);
			
			pl.getPlayer2().setBounds(posPlayer2.x - (pl.getPlayer2().getCenter().x), 
					posPlayer2.y - (pl.getPlayer2().getCenter().y) + pl.getPlayer1().getPreferredSize().height/2, 
					pl.getPlayer2().getPreferredSize().width, 
					pl.getPlayer2().getPreferredSize().height);
		}else{
			pl.getPlayer1().setBounds(posPlayer1.x - (pl.getPlayer1().getCenter().x), 
					posPlayer1.y - (pl.getPlayer1().getCenter().y), 
					pl.getPlayer1().getPreferredSize().width, 
					pl.getPlayer1().getPreferredSize().height);
			
			pl.getPlayer2().setBounds(posPlayer2.x - (pl.getPlayer2().getCenter().x), 
					posPlayer2.y - (pl.getPlayer2().getCenter().y), 
					pl.getPlayer2().getPreferredSize().width, 
					pl.getPlayer2().getPreferredSize().height);
		}
		pl.getPlayer1().repaint();
		pl.getPlayer2().repaint();
	}
	
	private void paintGameBoard(){
		ArrayList<GameField> gameBoard=null;
		try {
			gameBoard = GameBoard.getInstance().getGameBoard();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i=0;i<gameBoard.size();i++){
			c.add(gameBoard.get(i));
		}
	}
	
	public void dicesEnable(){
		this.dices.setEnabled(true);
		this.dicesItem.setEnabled(true);
	}
	public static int getSIZE() {
		return SIZE;
	}
	public void disableBuyButton(){
		buyStreet.setEnabled(false);
		buyItem.setEnabled(false);
	}
	public void enableBuyButton(){
		buyStreet.setEnabled(true);
		buyItem.setEnabled(true);
	}
	
	
}
